using System;
using System.Collections.Generic;
using System.Web.Security;
using Castle.Components.Validator;
using Castle.MonoRail.Framework;
using Query;
using Rhino.Commons;
using WineCellar.Data;
using WineCellar.Web.Filters;

namespace WineCellar.Web.Controllers
{
    [Layout("index"), AuthenticationFilter, Resource("cellar", "WineCellar.Web.Resources.Cellar")]      
    public class CellarController : ApplicationController
    {       
        public void List()
        {
            if (IsAjaxRequest) { CancelLayout(); }
            MembershipUser user = GetLoginMemberShipUser();
        	PropertyBag["cellars"] = Repository<Cellar>.FindAll(Where.Cellar.User.UserId.Eq(user.ProviderUserKey));                        
        }
        public void New()
        {
            if (IsAjaxRequest) { CancelLayout(); }         
        }
        public void Create([DataBind("cellar",Validate=true)]Cellar cellar)
        {
            if (!HasValidationError(cellar))
            {       
                MembershipUser user = GetLoginMemberShipUser();
				User myUser = Repository<User>.FindOne(Where.User.UserName.Eq(user.UserName));       
                Cellar existingCellar = Repository<Cellar>.FindOne(Where.Cellar.User.Eq(myUser));
                if (existingCellar == null)
                {
                    cellar.User = myUser;
                	cellar.DateAdded = DateTime.Now;
                	Repository<Cellar>.Save(cellar);                    
                    PropertyBag["updatedObject"] = cellar;
                }
                else
                {
                    ErrorSummary summary = new ErrorSummary();
                    summary.RegisterErrorMessage("User","Only one cellar allowed per user");
                    Flash["summary"] = summary;
                    Flash["updatedObject"] = cellar;
                }                                   
            }
            else 
            {
                Flash["cellar"] = cellar;
            	Flash["summary"] = GetErrorSummary(cellar);                  
            }
            RenderView("shared", "create");
        }
        public void Edit(int id)
        {
            if (IsAjaxRequest) { CancelLayout(); }
        	PropertyBag["cellar"] = Repository<Cellar>.Load(id);
        }
        public void Update([DataBind("cellar",Validate=true)] Cellar cellar)
        {
            if (!HasValidationError(cellar))
            {
            	With.Transaction(delegate
            	                 	{
										Repository<Cellar>.SaveOrUpdate(cellar);	
            	                 	});				
                PropertyBag["updatedObject"] = cellar;
            }
            else
            {
                Flash["updatedObject"] = cellar;
                Flash["summary"] = GetErrorSummary(cellar); 
            }
            RenderView("shared","update");
        }       
        public void Delete(int id)
        {
			Cellar cellar = Repository<Cellar>.Get(id);
			ICollection<CellarWine> cellarwines = Repository<CellarWine>.FindAll(Where.CellarWine.Cellar.Eq(cellar));
            if (cellarwines.Count <= 0)
            {
                With.Transaction(delegate
                                 	{
										Repository<Cellar>.DeleteAll(Where.Cellar.Id.Eq(id));	
                                 	});				
				PropertyBag["deletedObject"] = cellar;
            }
			else
            {
            	ErrorSummary summary = new ErrorSummary();
				summary.RegisterErrorMessage("Cellar","Unable to delete cellar, it already contains wines");
                Flash["deletedObject"] = cellar;
            	Flash["summary"] = summary;
            }
            RenderView("shared","delete");
        }
    }
}
